﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFServices.Commands;
using WPFServices.Models.Domain;
using WPFServices.Services.DomainService.BomInfo;
using WPFServices.Services.DomainService.ProcessRouteinfo;
using WPFServices.Services.DomainService.Productinfo;

namespace WPFServices.ViewModels.DomainViewModel.ProcessRouteinfo
{
    public class ProcessRouteViewModel:BaseViewModel
    {
        private readonly ProcessRouteService _processRouteService;
        private ObservableCollection<ProcessRouteDto> _processRoutes;
        private ProcessRouteDto _selectedProcessRoute; // 当前选中的工艺路线
        private bool _isLoading; // 数据加载状态
        private int _currentPage = 1; // 当前页码
        private int _pageSize = 10; // 每页大小
        private int _totalCount; // 总记录数
        private int _totalPages; // 总页数

        public ProcessRouteViewModel()
        {
            _processRouteService = new ProcessRouteService();
            _processRoutes = new ObservableCollection<ProcessRouteDto>();
            LoadDataCommand = new RelayCommand(async _ => await LoadProcessRouteAsync());
            SearchCommand = new RelayCommand(async _ => await SearchProcessRouteAsync());
            FirstPageCommand = new RelayCommand(_ => GoToFirstPage(), _ => CurrentPage > 1);
            PreviousPageCommand = new RelayCommand(_ => GoToPreviousPage(), _ => CurrentPage > 1);
            NextPageCommand = new RelayCommand(_ => GoToNextPage(), _ => CurrentPage < TotalPages);
            LastPageCommand = new RelayCommand(_ => GoToLastPage(), _ => CurrentPage < TotalPages);

            _ = LoadProcessRouteAsync();
        }

        #region 属性
        // 用于UI绑定的数据属性，通过SetProperty实现变更通知。
        public ObservableCollection<ProcessRouteDto> ProcessRoutes
        {
            get => _processRoutes;
            set => SetProperty(ref _processRoutes, value);
        }

        public ProcessRouteDto SelectedProcessRoute
        {
            get => _selectedProcessRoute;
            set => SetProperty(ref _selectedProcessRoute, value);
        }

        public bool IsLoading
        {
            get => _isLoading;
            set => SetProperty(ref _isLoading, value);
        }

        public int CurrentPage
        {
            get => _currentPage;
            set => SetProperty(ref _currentPage, value);
        }

        public int PageSize
        {
            get => _pageSize;
            set => SetProperty(ref _pageSize, value);
        }

        public int TotalCount
        {
            get => _totalCount;
            set => SetProperty(ref _totalCount, value);
        }

        public int TotalPages
        {
            get => _totalPages;
            set => SetProperty(ref _totalPages, value);
        }
        #endregion

        #region 命令
        // 用于UI绑定和触发ViewModel操作的命令。
        public RelayCommand LoadDataCommand { get; }
        public RelayCommand SearchCommand { get; }
        public RelayCommand FirstPageCommand { get; }
        public RelayCommand PreviousPageCommand { get; }
        public RelayCommand NextPageCommand { get; }
        public RelayCommand LastPageCommand { get; }
        #endregion

        #region 方法
        // 加载工艺路线数据，重置到第一页并触发搜索。
        private async Task LoadProcessRouteAsync()
        {
            CurrentPage = 1;
            await SearchProcessRouteAsync();
        }

        // 异步搜索工艺路线数据，并更新分页信息。
        private async Task SearchProcessRouteAsync()
        {
            try
            {
                IsLoading = true;
                var searchDto = new SearchProcessRouteDto
                {
                    PageIndex = CurrentPage,
                    PageSize = PageSize
                };

                var result = await _processRouteService.SearchProcessRouteAsync(searchDto);

                ProcessRoutes.Clear();
                if (result.Data != null)
                {
                    foreach (var processroute in result.Data)
                    {
                        ProcessRoutes.Add(processroute);
                    }
                }
                TotalCount = result.TotalCount;
                TotalPages = result.TotalPage;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show($"获取工艺路线数据失败: {ex.Message}", "错误");
            }
            finally
            {
                IsLoading = false;
            }
        }

        // 跳转到第一页。
        private void GoToFirstPage()
        {
            CurrentPage = 1;
            _ = SearchProcessRouteAsync();
        }

        // 跳转到上一页。
        private void GoToPreviousPage()
        {
            if (CurrentPage > 1)
            {
                CurrentPage--;
                _ = SearchProcessRouteAsync();
            }
        }

        // 跳转到下一页。
        private void GoToNextPage()
        {
            if (CurrentPage < TotalPages)
            {
                CurrentPage++;
                _ = SearchProcessRouteAsync();
            }
        }

        // 跳转到最后一页。
        private void GoToLastPage()
        {
            CurrentPage = TotalPages;
            _ = SearchProcessRouteAsync();
        }
        #endregion
    }
}
